package com.tacc.tyun.controller;

import com.tacc.tyun.base.BaseConstant;
import com.tacc.tyun.model.BaseDepartMenu;
import com.tacc.tyun.model.BaseMenu;
import com.tacc.tyun.model.SysOss;
import com.tacc.tyun.model.info.BaseUserInfo;
import com.tacc.tyun.service.BaseDepartMenuService;
import com.tacc.tyun.service.BaseMenuService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
* 首页跳转
* @author congcong tao
* @date 2017/7/28 16:37
*/
@Controller
public class CommonController extends BaseController{
    private static Logger log= LoggerFactory.getLogger(CommonController.class);
    @Resource
    private BaseMenuService baseMenuService;
    @Resource
    private BaseDepartMenuService baseDepartMenuService;

    /**
     * 个人中心页面
     * */
    @RequestMapping(value="/index")
    public ModelAndView Index(HttpServletRequest request, HttpServletResponse response){
        BaseUserInfo baseUserInfo=acceptCurrentUser(request);
        //菜单查询
        BaseDepartMenu baseDepartMenu=new BaseDepartMenu();
        baseDepartMenu.setDepartid(baseUserInfo.getDepartid());
        baseDepartMenu.setRemovetag(BaseConstant.REMOVE_N);
        List<BaseDepartMenu> baseDepartMenuList=baseDepartMenuService.select(baseDepartMenu);

        BaseMenu baseMenu=new BaseMenu();
        baseMenu.setRemovetag(BaseConstant.REMOVE_N);
        List<BaseMenu> bList=baseMenuService.select(baseMenu);

        List<BaseMenu> baseMenusList=new ArrayList<BaseMenu>();
        if(baseDepartMenuList !=null && baseDepartMenuList.size() > 0){
            for (BaseDepartMenu baseDepartMenu1:baseDepartMenuList){
                for (BaseMenu baseMenu1:bList){
                    if(baseMenu1.getMenuid().equals(baseDepartMenu1.getMenuid())){
                        baseMenusList.add(baseMenu1);
                        break;
                    }
                }
            }
        }
        String baseUrl=null;
        List<BaseMenu> baseMenusListOne=new ArrayList<BaseMenu>();
        if(baseMenusList!=null && baseMenusList.size() > 0){
            for(BaseMenu baseMenu1:baseMenusList){
                if(BaseConstant.MENU_ONE.equals(baseMenu1.getMenugrade())){
                    List<BaseMenu> baseMenusListTwo=new ArrayList<BaseMenu>();
                    for(BaseMenu baseMenu2:baseMenusList){
                        if(baseMenu1.getMenuid().equals(baseMenu2.getParentmenuid())){
                            baseMenusListTwo.add(baseMenu2);
                        }
                    }
                    if(baseMenusListOne.size()==0){
                        if(baseMenusListTwo!=null && baseMenusListTwo.size()>0){
                            baseUrl=baseMenusListTwo.get(0).getMenuurl();
                            if(baseUrl.contains("?")){
                                baseUrl=baseUrl+"&menuid="+baseMenusListTwo.get(0).getMenuid();
                            }else{
                                baseUrl=baseUrl+"?menuid="+baseMenusListTwo.get(0).getMenuid();
                            }
                        }else{
                            baseUrl=baseMenu1.getMenuurl();
                            if(baseUrl.contains("?")){
                                baseUrl=baseUrl+"&menuid="+baseMenu1.getMenuid();
                            }else{
                                baseUrl=baseUrl+"?menuid="+baseMenu1.getMenuid();
                            }
                        }
                    }
                    baseMenu1.setChildList(baseMenusListTwo);
                    baseMenusListOne.add(baseMenu1);
                }
            }
        }
        return new ModelAndView("/index").addObject("baseMenusListOne",baseMenusListOne).addObject("baseUrl",baseUrl).addObject("baseUserInfo",baseUserInfo);
    }

    /**
     * 首页
     * */
    @RequestMapping(value="/home")
    public ModelAndView Home(HttpServletRequest request, HttpServletResponse response){
        // UserInfo userInfo=AcceptUserInfo(request,response);
        return new ModelAndView("/home");
    }

    /**
     * 爱情
     **/
    @RequestMapping(value = "/love")
    public ModelAndView loveyun(){
        String yunyun = (String) SecurityUtils.getSubject().getSession().getAttribute("yunyun");
        System.out.print(yunyun);
        return new ModelAndView("love");
    }

    /**
     * 踢出登录
     **/
    @RequestMapping(value = "/kickout")
    public ModelAndView kickout(){
        return new ModelAndView("login/page");
    }

    //获取当前登录用户信息
    public static BaseUserInfo acceptCurrentUser(HttpServletRequest request){
        return (BaseUserInfo) request.getSession().getAttribute(BaseConstant.SESSION_USERINFO);
    }
}
